﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Assist;
using YArchitech.Controls;
using YArchitech.DataModel;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;

namespace YArchitech.Plumbing
{
	public partial class FormProtectRadiusCalc : System.Windows.Forms.Form
	{
		public FormProtectRadiusCalc()
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			base.Icon = Resource.YArchitechIcon;
			this.InitData();
			this.InitParameterSetting();
		}

		private void InitData()
		{
			this.cbxLength.Items.AddRange(new object[]
			{
				"10",
				"15",
				"20",
				"25",
				"30"
			});
			this.cbxMaterial.Items.AddRange(new object[]
			{
				"帆布",
				"麻织",
				"衬胶"
			});
			this.cbxType.Items.AddRange(new object[]
			{
				"50",
				"65"
			});
			this.cbxDiameter.Items.AddRange(new object[]
			{
				"13",
				"16",
				"19"
			});
			this.cbxMinLength.Items.AddRange(new object[]
			{
				"6",
				"7",
				"8",
				"10",
				"11",
				"12",
				"13",
				"14",
				"15",
				"16"
			});
		}

		private void InitParameterSetting()
		{
			this.hyFireData = this.userSetting.GetProtectRadiusSet();
			this.cbxLength.Text = this.hyFireData.ILength.ToString();
			this.cbxMaterial.SelectedIndex = int.Parse(this.hyFireData.IMaterial.ToString());
			this.cbxDiameter.SelectedIndex = int.Parse(this.hyFireData.IDiameter.ToString());
			this.cbxType.SelectedIndex = int.Parse(this.hyFireData.IType.ToString());
			this.hyTxtMinFlow.Text = this.hyFireData.DMinFlow.ToString();
			this.cbxMinLength.Text = this.hyFireData.DMinLength.ToString();
			if (int.Parse(this.hyFireData.IKnow.ToString()) == 0)
			{
				this.rdoMinFlow.Checked = true;
			}
			else
			{
				this.rdoMinLength.Checked = true;
			}
			this.hyTxtFlow.Text = this.hyFireData.DFlow.ToString();
			this.hyTxtPressure.Text = this.hyFireData.DPressure.ToString();
			this.hyTxtRadius.Text = this.hyFireData.DRadius.ToString();
		}

		private void btnCalc_Click(object sender, EventArgs e)
		{
			if (this.cbxLength.Text.Trim() == "" || (this.rdoMinFlow.Checked && this.hyTxtMinFlow.Text.Trim() == "") || (this.rdoMinLength.Checked && this.cbxMinLength.Text.Trim() == ""))
			{
				YJKMessageBox.Show("参数不能为空");
				return;
			}
			this.UserSettingWrite();
			this.Calculate();
		}

		private void btnOk_Click(object sender, EventArgs e)
		{
			if (this.cbxLength.Text.Trim() == "" || (this.rdoMinFlow.Checked && this.hyTxtMinFlow.Text.Trim() == "") || (this.rdoMinLength.Checked && this.cbxMinLength.Text.Trim() == ""))
			{
				YJKMessageBox.Show("参数不能为空");
				return;
			}
			this.UserSettingWrite();
			this.Calculate();
			base.DialogResult = DialogResult.OK;
		}

		private void Calculate()
		{
			HYHydrantCal hyhydrantCal = new HYHydrantCal();
			this.hyFireData = hyhydrantCal.GetFireData(this.hyFireData);
			this.hyTxtPressure.Text = this.hyFireData.DPressure.ToString("0.00");
			if (this.hyFireData.IKnow == 0)
			{
				this.cbxMinLength.Text = this.hyFireData.DMinLength.ToString("0.00");
			}
			this.hyTxtFlow.Text = this.hyFireData.DFlow.ToString("0.00");
			this.dRadius = hyhydrantCal.GetProtectRadius((double)this.hyFireData.ILength, this.hyFireData.DMinLength);
			this.hyTxtRadius.Text = this.dRadius.ToString("0.00");
			this.UserSettingWrite();
			this.userSetting.WriteProtectRadiusSet(this.hyFireData);
		}

		private void UserSettingWrite()
		{
			this.hyFireData.ILength = int.Parse(this.cbxLength.Text);
			this.hyFireData.IMaterial = this.cbxMaterial.SelectedIndex;
			this.hyFireData.IType = this.cbxType.SelectedIndex;
			this.hyFireData.IDiameter = this.cbxDiameter.SelectedIndex;
			this.hyFireData.DMinFlow = double.Parse(this.hyTxtMinFlow.Text);
			this.hyFireData.DMinLength = double.Parse(this.cbxMinLength.Text);
			this.hyFireData.IKnow = (this.rdoMinFlow.Checked ? 0 : 1);
			this.hyFireData.DFlow = double.Parse(this.hyTxtFlow.Text);
			this.hyFireData.DPressure = double.Parse(this.hyTxtPressure.Text);
			this.hyFireData.DRadius = double.Parse(this.hyTxtRadius.Text);
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private void cbxLength_KeyPress(object sender, KeyPressEventArgs e)
		{
			if (!char.IsNumber(e.KeyChar) && e.KeyChar.ToString() != "\b")
			{
				e.Handled = true;
			}
		}

		private void hyTxtRadius_KeyPress(object sender, KeyPressEventArgs e)
		{
			e.Handled = true;
		}

		private void hyTxtPressure_KeyPress(object sender, KeyPressEventArgs e)
		{
			e.Handled = true;
		}

		private void hyTxtFlow_KeyPress(object sender, KeyPressEventArgs e)
		{
			e.Handled = true;
		}

		private void rdoMinFlow_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rdoMinFlow.Checked)
			{
				this.hyTxtMinFlow.Enabled = true;
				this.cbxMinLength.Enabled = false;
			}
		}

		private void rdoMinLength_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rdoMinLength.Checked)
			{
				this.hyTxtMinFlow.Enabled = false;
				this.cbxMinLength.Enabled = true;
			}
		}

		private HVACUserSetting userSetting = new HVACUserSetting();

		public HYFireData hyFireData = new HYFireData();

		public double dRadius;
	}
}
